package com.bysj.lms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bysj.lms.pojo.entity.Role;
import com.bysj.lms.pojo.entity.RolePermission;
import com.bysj.lms.mapper.RolePermissionMapper;
import com.bysj.lms.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public Set<Integer> getPidByRid(List<Role> roles) {
        HashSet<Integer> pidSet = new HashSet<>();
        roles.forEach(role ->
        {
            QueryWrapper<RolePermission> query = new QueryWrapper<>();
            query.eq("role_id",role.getId());
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(query);
            rolePermissions.forEach(rp->pidSet.add(rp.getPermissionId()));
        });
        return pidSet;
    }
}
